package com.fastloan.mgt.application.approve.service.impl;

import com.fastloan.common.BasicServiceImpl;
import com.fastloan.mgt.application.approve.dao.ApplicationApproveFileMapper;
import com.fastloan.mgt.application.approve.model.ApplicationApprove;
import com.fastloan.mgt.application.approve.model.ApplicationApproveTemplate;
import com.fastloan.mgt.application.approve.service.ApplicationApproveFileService;
import com.fastloan.mgt.application.approve.model.ApplicationApproveFile;
import com.fastloan.mgt.application.approve.service.ApplicationApproveService;
import com.fastloan.mgt.application.approve.service.ApplicationApproveTemplateService;
import com.kawins.supers.file.IFileManager;
import com.kawins.supers.file.core.FileLoadBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

import static java.util.stream.Collectors.toList;

/**
 * @author admins
 * createTime 2017/11/09
 */
@Service
public class ApplicationApproveFileServiceImpl extends BasicServiceImpl<ApplicationApproveFile> implements ApplicationApproveFileService {

    @Autowired
    private IFileManager fileManager;

    @Autowired
    private ApplicationApproveService applicationApproveService;

    @Autowired
    private ApplicationApproveTemplateService applicationApproveTemplateService;


    @Override
    @Transactional
    public List<FileLoadBean> uploadApproveFiles(String appApproveId, MultipartFile[] files) {
        List<FileLoadBean> upload = fileManager.upload(files);
        if (upload != null && !upload.isEmpty()) {
            upload.forEach(fileLoadBean -> {
                ApplicationApproveFile applicationApproveFile = new ApplicationApproveFile();
                applicationApproveFile.setFileId(fileLoadBean.getFileId().toString());
                applicationApproveFile.setAppApproveId(appApproveId);
                insert(applicationApproveFile);
            });
        }
        return upload;
    }

    @Override
    @Transactional
    public Integer deleteApproveFiles(String appApproveId, String fileId) {
        deleteByPrimaryKey(appApproveId);
        return fileManager.delete(fileId);
    }

    @Override
    public List<String> getApproveFileIds(String applicationId) {
        List<String> fileIds = new ArrayList<>();
        ApplicationApproveTemplate approveTemplate = new ApplicationApproveTemplate();
        approveTemplate.setApplicationId(applicationId);
        List<ApplicationApproveTemplate> approveTemplateList = applicationApproveTemplateService.select(approveTemplate);
        if (!approveTemplateList.isEmpty()) {
            List<List<List<String>>> listList = approveTemplateList.stream()
                    .map(applicationApproveTemplate -> {
                        ApplicationApprove applicationApprove = new ApplicationApprove();
                        applicationApprove.setTemplateId(applicationApproveTemplate.getTemplateId());
                        List<ApplicationApprove> approveList = applicationApproveService.select(applicationApprove);
                        return approveList.stream()
                                .map(approveListFunction -> {
                                    ApplicationApproveFile applicationApproveFile = new ApplicationApproveFile();
                                    applicationApproveFile.setAppApproveId(approveListFunction.getAppApproveId());
                                    List<ApplicationApproveFile> approveFileList = select(applicationApproveFile);
                                    return approveFileList.stream()
                                            .filter(Objects::nonNull)
                                            .map(ApplicationApproveFile::getFileId)
                                            .collect(toList());
                                }).collect(toList());
                    }).collect(toList());
            listList.stream()
                    .filter(Objects::nonNull)
                    .forEach(lists -> lists.stream()
                            .filter(Objects::nonNull)
                            .forEach(fileIds::addAll));
        }
        return fileIds.isEmpty() ? new ArrayList<>() : fileIds;
    }
}
